package com.geeklei.media.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.widget.RelativeLayout;

import com.geeklei.media.ui.R;

public class GestureVolumn extends GestureView {
    public GestureVolumn(Context context) {
        super(context);
    }

    public static GestureVolumn create(RelativeLayout relativeLayout) {
        GestureVolumn gestureVolumn = new GestureVolumn(relativeLayout.getContext());
        gestureVolumn.setLayoutParams(new LayoutParams(-1, -1));
        relativeLayout.addView(gestureVolumn);
        return gestureVolumn;
    }

    public void adjustVolume(Activity activity, float f) {
        AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
        int streamMaxVolume = audioManager.getStreamMaxVolume(3);
        int newVolumeValue = getNewVolumeValue(f, streamMaxVolume, audioManager.getStreamVolume(3));
        audioManager.setStreamVolume(3, newVolumeValue, 8);
        if (streamMaxVolume != 0) {
            setPercent((newVolumeValue * 100) / streamMaxVolume);
        }
        show();
    }

    private void setPercent(int i) {
        this.mText.setText(i + "%");
    }

    private int getNewVolumeValue(float f, int i, int i2) {
        int r0 = f > 0.0f ? i2 - 1 : f < 0.0f ? i2 + 1 : i2;
        if (r0 > i) {
            r0 = i;
        }
        if (r0 < 0) {
            return 0;
        }
        return r0;
    }

    protected int getIcon() {
        return R.drawable.vp_vol_icon_big;
    }

    protected int getIconMarginTop() {
        return getResources().getDimensionPixelSize(R.dimen.vp_volumn_icon_margin_top);
    }

    protected int getTextMarginTop() {
        return getResources().getDimensionPixelSize(R.dimen.vp_volumn_percent_margin_top);
    }
}
